using UnityEngine;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;

//using IronPython;
//using IronPython.Hosting;
//using Microsoft.Scripting;
//using Microsoft.Scripting.Hosting;

using Calibur;
using com.example.eggworld;
using EggWorld.Protocol;

public class RealPlayer : Player {
	long id;
	string name;
	
	public long Id {
		get { return id; }
		set { id = value; }
	}
	
	public string Name {
		get { return name; }
		set { name = value; }
	}

	public RealPlayer(long id, string name) {
		this.id = id;
		this.name = name;
	}

	public long getId() {
		return id;
	}

	public string getName() {
		return name;
	}
}

public class ServerLink : MonoBehaviour {
	private Session session;

	// Use this for initialization
	public void Start() {
//		PlayerAI playerAi = new PlayerAI();
//		Player player = new RealPlayer(45678, "benjamin");
//		print("result=" + playerAi.attack(player));
		
	}
	
	// Update is called once per frame
	public void Update() {
		compile();
		MessageBean mbean;
		if (session != null && session.Established) {
			mbean = session.RecvMessageBean();
			if (mbean != null) {
				print(mbean.ToString());
			}
		}
	}
	
	private void compile() {
		print("Enter compile ...");
//		Assembly aiAss = Assembly.LoadFrom(@"D:\CaliburProjects\AI\ai.dll");
//		System.Type aiType = aiAss.GetType("com.example.eggworld.PlayerAI");
//		print("aiType=" + aiType);
		
		
//		ScriptEngine _engine;
//		ScriptRuntime _runtime;
//		CompiledCode _code;
//		ScriptScope _scope;
//
//		string source = @"d:\tmp\test.py";
//		_engine = Python.CreateEngine();
//		_runtime = _engine.Runtime;
//		_scope = _engine.CreateScope();
//			
//		ScriptSource _script = _engine.CreateScriptSourceFromFile(source, Encoding.UTF8, SourceCodeKind.File);
//		_code = _script.Compile(); // error when running this line
	}
	
	public void Connect() {
		// Create session
		session = new Session(new ObjectFactory());

		
		session.Open("10.228.105.70", 10000);
		
		// Login player
		if (session.Established) {
			Login login = new Login(1000);
			login.Passport = "john";
			login.Password = "123456";
			session.SendMessageBean(login);
		} else {
			print("Create session failed.");
			session.Close();
			session = null;
		}
	}
}
